--[[
  江西银行 AFANATP - 7001端口
]]
require("apm_core")

local G_DEBUG = true

if G_DEBUG == false then
    function logDebug(fmt,...) end
    function logInfo(fmt,...) end
    function logWarn(fmt,...) end
    function logError(fmt,...) end
    function logFatal(fmt,...) end
end

local KRED='\x1B[0;31m'
local KSALMON='\x1B[1;31m'
local KGREEN='\x1B[0;32m'
local KLIMEGREEN='\x1B[1;32m'
local KYELLOW='\x1B[0;33m'
local KORANGE='\x1B[0;34m'
local KBLUE='\x1B[1;34m'
local KMAGENTA='\x1B[0;35m'
local KPINK='\x1B[1;35m'
local KCYAN='\x1B[0;36m'
local RESET='\x1B[0m'

local dict_transname = {
    ["ibps.101.001.01"] = "网银贷记业务报文",
    ["ibps.102.001.01"] = "网银贷记业务回执报文",
    ["ibps.103.001.01"] = "网银借记业务报文",
    ["ibps.104.001.01"] = "网银借记业务回执报文",
    ["ibps.105.001.01"] = "第三方贷记业务报文",
    ["ibps.106.001.01"] = "第三方贷记业务回执报文",
    ["ccms.303.001.01"] = "自由格式报文",
    ["ibps.305.001.01"] = "业务状态查询报文",
    ["ibps.306.001.01"] = "业务状态查询应答报文",
    ["ibps.307.001.01"] = "业务撤销申请报文",
    ["ibps.308.001.01"] = "业务撤销应答报文",
    ["ibps.309.001.01"] = "账户余额查询申请报文",
    ["ibps.310.001.01"] = "账户余额查询应答报文",
    ["ibps.311.001.01"] = "账户交易明细查询申请报文",
    ["ibps.312.001.01"] = "账户交易明细查询应答报文",
    ["ibps.331.001.01"] = "预授权申请报文",
    ["ibps.332.001.01"] = "预授权应答报文",
    ["ibps.333.001.01"] = "预授权撤销申请报文",
    ["ibps.334.001.01"] = "预授权撤销应答报文",
    ["ibps.335.001.01"] = "授权支付协议管理申请报文",
    ["ibps.336.001.01"] = "授权支付协议管理应答报文",
    ["ibps.337.001.01"] = "授权支付协议管理通知报文",
    ["ibps.338.001.01"] = "授权支付协议管理通知应答报文",
    ["ibps.339.001.01"] = "账户信息查询协议管理申请报文",
    ["ibps.340.001.01"] = "账户信息查询协议管理应答报文",
    ["ibps.341.001.01"] = "账户信息查询协议管理通知报文",
    ["ibps.342.001.01"] = "账户信息查询协议管理通知应答报文",
    ["saps.601.001.01"] = "轧差通知报文",
    ["saps.603.001.01"] = "清算通知报文",
    ["saps.605.001.01"] = "计费与返还清单报文",
    ["ibps.701.001.01"] = "汇总核对报文",
    ["ibps.703.001.01"] = "业务明细核对申请报文",
    ["ibps.704.001.01"] = "业务明细核对应答报文",
    ["ibps.705.001.01"] = "业务明细核对下载申请报文",
    ["ibps.706.001.01"] = "业务明细核对下载应答报文",
    ["ccms.801.001.01"] = "系统状态变更通知报文",
    ["ccms.803.001.01"] = "参与机构状态变更通知报文",
    ["ccms.805.001.01"] = "登录/退出申请报文",
    ["ccms.806.001.01"] = "登录/退出应答报文",
    ["ccms.807.001.01"] = "强制离线通知报文",
    ["ccms.809.001.01"] = "停运通知报文",
    ["ccms.900.001.01"] = "通用处理确认报文",
    ["ibps.901.001.01"] = "行别变更通知报文",
    ["ccms.903.001.01"] = "数字证书绑定通知报文",
    ["ccms.905.001.01"] = "参与机构变更通知报文",
    ["ccms.907.001.01"] = "系统参数变更通知报文",
    ["ccms.909.001.01"] = "支付业务金额上限变更通知报文",
    ["ccms.911.001.01"] = "报文丢弃通知报文",
    ["ccms.990.001.01"] = "通信级确认报文",
}

function post_analysis()
    local ApmData = require("ApmData")
    if ApmData.getMsgType() ~= 1 then return end

    local s_transcode
    local s_transname
    local s_channelcode
    local s_channelname
    local s_flowno
    local s_rspcode
    local s_rspmsg

    -- 交易类型
    s_transcode = ApmData.getField("transaction.type_code", 1)
    if s_transcode ~= nil and s_transcode ~= "" then
        s_transname = dict_transname[s_transcode]
        if s_transname == nil then s_transname = s_transcode end
    end

    -- 渠道类型

    -- 返回码
    s_rspcode = ApmData.getField("response.jxbank_afanatp.errorcode", 1)
    s_rspmsg = ApmData.getField("response.jxbank_afanatp.errormsg", 1)

    if s_transcode == nil or s_transcode == "" then
        logInfo("s_transcode is nil [%s]", ApmData.dumpFields(true))
--    elseif s_channelcode == nil then
--        logInfo("s_channelcode is nil [%s]", ApmData.dumpFields(true))
    elseif s_rspcode == nil then
        logInfo("s_rspcode is nil [%s]", ApmData.dumpFields(true))
    else
        ApmData.replaceField("transaction.type_code", s_transcode)
        ApmData.replaceField("transaction.type_name", s_transname)
        ApmData.replaceField("transaction.title", s_transcode)

        if s_channelcode ~= nil and s_channelcode ~= "" then
            ApmData.replaceField("transaction.channel_code", s_channelcode)
            ApmData.replaceField("transaction.channel_name", s_channelcode)
        end

        ApmData.replaceField("response.code_string", s_rspcode)
        ApmData.delFields("response.code")

        if s_rspmsg ~= nil then
            ApmData.replaceField("response.error_message", s_rspmsg)
        end

        logInfo("AFANATP 7001(%s:%d->%s:%d) name:%s|channel:%s|seq:%s|ret:%s|%s",
            ApmData.getSrcIp(),
            ApmData.getSrcPort(),
            ApmData.getDstIp(),
            ApmData.getDstPort(),
            s_transname,
            s_channelcode,
            s_flowno,
            s_rspcode,
            s_rspmsg
        )
    end

    return
end
--AFANATP
--10.1.1.86:7001
--v1.0.0
